import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import "components"
import "styles"

Rectangle {
    id: operationRecordsPage
    width: 1024
    height: 600
    color: AppStyle.backgroundColor

    property string selectedDateFrom: ""
    property string selectedDateTo: ""
    property string selectedUser: ""
    property string selectedAction: ""

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 20

        // 标题和返回按钮
        RowLayout {
            Layout.fillWidth: true

            Text {
                text: "操作记录"
                font.pixelSize: 24
                font.bold: true
                color: AppStyle.textColor
                Layout.fillWidth: true
            }

            CustomButton {
                text: "导出记录"
                onClicked: {
                    if (qmlBridge) {
                        qmlBridge.exportAuditLogs("audit_logs.csv", "CSV")
                    }
                }
            }

            CustomButton {
                text: "返回"
                backgroundColor: AppStyle.secondaryColor
                onClicked: {
                    if (qmlBridge) {
                        qmlBridge.navigateToPage("Dashboard")
                    }
                }
            }
        }

        // 筛选条件
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 120
            color: AppStyle.cardBackgroundColor
            radius: 8
            border.color: AppStyle.borderColor
            border.width: 1

            GridLayout {
                anchors.fill: parent
                anchors.margins: 20
                columns: 4
                columnSpacing: 15
                rowSpacing: 10

                Text {
                    text: "开始日期:"
                    font.pixelSize: 14
                    color: AppStyle.textColor
                }

                CustomTextField {
                    id: dateFromField
                    Layout.preferredWidth: 120
                    placeholderText: "YYYY-MM-DD"
                    text: operationRecordsPage.selectedDateFrom
                }

                Text {
                    text: "结束日期:"
                    font.pixelSize: 14
                    color: AppStyle.textColor
                }

                CustomTextField {
                    id: dateToField
                    Layout.preferredWidth: 120
                    placeholderText: "YYYY-MM-DD"
                    text: operationRecordsPage.selectedDateTo
                }

                Text {
                    text: "用户:"
                    font.pixelSize: 14
                    color: AppStyle.textColor
                }

                ComboBox {
                    id: userComboBox
                    Layout.preferredWidth: 120
                    model: ["全部", "admin", "operator", "system"]
                    currentIndex: 0
                }

                Text {
                    text: "操作类型:"
                    font.pixelSize: 14
                    color: AppStyle.textColor
                }

                ComboBox {
                    id: actionComboBox
                    Layout.preferredWidth: 120
                    model: ["全部", "登录", "登出", "采样", "设置", "用户管理"]
                    currentIndex: 0
                }

                Item {
                    Layout.columnSpan: 2
                }

                CustomButton {
                    text: "搜索"
                    Layout.columnSpan: 2
                    onClicked: {
                        operationRecordsPage.selectedDateFrom = dateFromField.text
                        operationRecordsPage.selectedDateTo = dateToField.text
                        operationRecordsPage.selectedUser = userComboBox.currentText
                        operationRecordsPage.selectedAction = actionComboBox.currentText
                        refreshRecords()
                    }
                }
            }
        }

        // 记录列表
        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            color: AppStyle.cardBackgroundColor
            radius: 8
            border.color: AppStyle.borderColor
            border.width: 1

            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 20
                spacing: 10

                // 表头
                Rectangle {
                    Layout.fillWidth: true
                    height: 40
                    color: AppStyle.headerBackgroundColor
                    radius: 4

                    RowLayout {
                        anchors.fill: parent
                        anchors.margins: 10
                        spacing: 10

                        Text {
                            text: "时间"
                            font.pixelSize: 14
                            font.bold: true
                            color: AppStyle.textColor
                            Layout.preferredWidth: 140
                        }

                        Text {
                            text: "用户"
                            font.pixelSize: 14
                            font.bold: true
                            color: AppStyle.textColor
                            Layout.preferredWidth: 100
                        }

                        Text {
                            text: "操作"
                            font.pixelSize: 14
                            font.bold: true
                            color: AppStyle.textColor
                            Layout.preferredWidth: 120
                        }

                        Text {
                            text: "表名"
                            font.pixelSize: 14
                            font.bold: true
                            color: AppStyle.textColor
                            Layout.preferredWidth: 100
                        }

                        Text {
                            text: "记录ID"
                            font.pixelSize: 14
                            font.bold: true
                            color: AppStyle.textColor
                            Layout.preferredWidth: 80
                        }

                        Text {
                            text: "描述"
                            font.pixelSize: 14
                            font.bold: true
                            color: AppStyle.textColor
                            Layout.fillWidth: true
                        }
                    }
                }

                // 记录列表
                ListView {
                    id: recordsListView
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    model: qmlBridge ? qmlBridge.getAuditLogs(
                        operationRecordsPage.selectedDateFrom,
                        operationRecordsPage.selectedDateTo,
                        operationRecordsPage.selectedUser === "全部" ? "" : operationRecordsPage.selectedUser,
                        operationRecordsPage.selectedAction === "全部" ? "" : operationRecordsPage.selectedAction
                    ) : []
                    
                    delegate: Rectangle {
                        width: parent.width
                        height: 50
                        color: index % 2 === 0 ? "transparent" : AppStyle.alternateRowColor

                        RowLayout {
                            anchors.fill: parent
                            anchors.margins: 10
                            spacing: 10

                            Text {
                                text: modelData.timestamp || ""
                                font.pixelSize: 12
                                color: AppStyle.textColor
                                Layout.preferredWidth: 140
                            }

                            Text {
                                text: modelData.userId || ""
                                font.pixelSize: 12
                                color: AppStyle.textColor
                                Layout.preferredWidth: 100
                            }

                            Text {
                                text: modelData.action || ""
                                font.pixelSize: 12
                                color: getActionColor(modelData.action)
                                Layout.preferredWidth: 120
                            }

                            Text {
                                text: modelData.tableName || ""
                                font.pixelSize: 12
                                color: AppStyle.textColor
                                Layout.preferredWidth: 100
                            }

                            Text {
                                text: modelData.recordId || ""
                                font.pixelSize: 12
                                color: AppStyle.textColor
                                Layout.preferredWidth: 80
                            }

                            Text {
                                text: modelData.description || ""
                                font.pixelSize: 12
                                color: AppStyle.secondaryTextColor
                                Layout.fillWidth: true
                                wrapMode: Text.WordWrap
                            }
                        }
                    }
                }

                // 分页控制
                RowLayout {
                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignHCenter
                    spacing: 10

                    CustomButton {
                        text: "上一页"
                        enabled: false // 暂时禁用，后续可实现分页功能
                    }

                    Text {
                        text: "第 1 页，共 " + (recordsListView.model ? recordsListView.model.length : 0) + " 条记录"
                        font.pixelSize: 12
                        color: AppStyle.secondaryTextColor
                    }

                    CustomButton {
                        text: "下一页"
                        enabled: false // 暂时禁用，后续可实现分页功能
                    }
                }
            }
        }
    }

    function getActionColor(action) {
        switch(action) {
            case "LOGIN":
                return AppStyle.successColor
            case "LOGOUT":
                return AppStyle.warningColor
            case "CREATE":
                return AppStyle.primaryColor
            case "UPDATE":
                return AppStyle.infoColor
            case "DELETE":
                return AppStyle.dangerColor
            case "SYSTEM":
                return AppStyle.secondaryTextColor
            default:
                return AppStyle.textColor
        }
    }

    function refreshRecords() {
        // 刷新记录列表
        if (recordsListView.model) {
            recordsListView.model = qmlBridge.getAuditLogs(
                operationRecordsPage.selectedDateFrom,
                operationRecordsPage.selectedDateTo,
                operationRecordsPage.selectedUser === "全部" ? "" : operationRecordsPage.selectedUser,
                operationRecordsPage.selectedAction === "全部" ? "" : operationRecordsPage.selectedAction
            )
        }
    }

    Component.onCompleted: {
        // 设置默认日期范围（最近7天）
        var today = new Date()
        var weekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000)
        
        operationRecordsPage.selectedDateFrom = Qt.formatDate(weekAgo, "yyyy-MM-dd")
        operationRecordsPage.selectedDateTo = Qt.formatDate(today, "yyyy-MM-dd")
        
        dateFromField.text = operationRecordsPage.selectedDateFrom
        dateToField.text = operationRecordsPage.selectedDateTo
        
        refreshRecords()
    }
}